JunYingScene={}
JunYingSceneFunc={}
JunYingSceneFunc.__index = JunYingSceneFunc
setmetatable(JunYingScene, JunYingSceneFunc)
JunYingScene.NodeID = {UpButton=1020}
--JunYingScene.upgradeCooldown = 100

JunYingSceneFunc.getNode = function()
  local l = {}
  setmetatable(l, JunYingSceneFunc)
  l.node = CCBReader:load("JunYingScene.ccbi")
  print("JunYingScene.getNode", l.node)
  JunYingScene.instance = l
  l:init() 
  return l 
end

JunYingSceneFunc.getScene = function()
  local sc = CCScene:create()
  sc:addChild(JunYingScene.getNode().node)
  print("JunYingScene.getScene", sc)
  return sc
end

JunYingSceneFunc.init = function(self)
  local b = self.node:getChildByTag(JunYingScene.NodeID.UpButton)
  setObjectExtTable(b, self)
  b = self.node:getChildByTag(1111)
  setObjectExtTable(b, self)

  setBuildingBaseData(BuildingID.JunYing, self.node)
  --UpgradeManager.checkUpgrade(BuildingID.JunYing)
  self:setLimitLabel()
  
  self.upBuildingButton = TypeCast(self.node:getChildByTag(1020), "CCButton")
  self.timeLabel = TypeCast(self.node:getChildByTag(1006), "CCLabelTTF")
  local tmp = UITimer.getUITimer(string.format("upbuildingcd_%d", BuildingID.JunYing), self, ccp(160,72), ccp(0, 0))
  if tmp~=nil then
    tmp.view:setPosition(ccp(710, 420))
    self.node:addChild(tmp.view)
    self.upBuildingTimer = tmp
    self.timeLabel:setVisible(false)
    self.upBuildingButton:setVisible(false)
    self.upBuildingButton:setEnabled(false)
  end
end

JunYingSceneFunc.setLimitLabel = function(self)
  local bl = UserData.userinfo.host.cities2[tostring(BuildingID.JunYing)].l
  local binfo = UserData.config.models[BuildingLevel[BuildingID.JunYing]][bl]
  local binfo1 = UserData.config.models[BuildingLevel[BuildingID.JunYing]][bl+1]

  local lb = self.node:getChildByTag(1008)
  TypeCast(lb, "CCLabelTTF"):setString(tostring(binfo.soldierMax))
  lb = self.node:getChildByTag(1009)
  TypeCast(lb, "CCLabelTTF"):setString(tostring(binfo1.soldierMax))
end

JunYingSceneFunc.onUpgradeButton = function(self, node, event)
  if JunYingScene.upgradeCooldown==nil or JunYingScene.upgradeCooldown<=0 then
    --setTouchEnabledAll(self.node, false)
    LoadScene:showLoadingScene()
    NetworkRequest.upgradeBuilding(BuildingID.JunYing, "onUpgradeZhiHuiBuComplete", self)
    --self.upBuildingButton:setEnabled(false)
  else
    --JiaSuDialog:create(BuildingID.JunYing, self)
  end 
end
JunYingSceneFunc.onUpgradeZhiHuiBuComplete = function(self, data)
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    showMessage("升级成功", ccc3(0,255,0))
    
    local bl = UserData.userinfo.host.cities2[tostring(BuildingID.JunYing)].l
    local binfo = UserData.config.models[BuildingLevel[BuildingID.JunYing]][bl]
    UserData.userinfo.host.cities2[tostring(BuildingID.JunYing)].l = bl+1
    setBuildingBaseData(BuildingID.JunYing, self.node)
    self:setLimitLabel()
    
    self.upBuildingTimer = UITimer.createUITimer(string.format("upbuildingcd_%d", BuildingID.JunYing), binfo.coolingTime, self, ccp(160,72), ccp(0, 0))
    self.upBuildingTimer.view:setPosition(ccp(710, 420))
    self.node:addChild(self.upBuildingTimer.view)
    self.timeLabel:setVisible(false)
    self.upBuildingButton:setVisible(false)
  else
    showMessage(res.msg, ccc3(255,255,0))
    self.upBuildingButton:setEnabled(true)
    --local b = self.node:getChildByTag(JunYingScene.NodeID.UpButton)
    --TypeCast(b, "CCButton"):setEnabled(true)
  end
  --setTouchEnabledAll(self.node, true)
  LoadScene:removeLoadingScene()
  self:closeScene()
  MainScene.scene:addChild(JunYingScene:getNode().node)
end
---------------------------------------------------------------------------------------------------------------
--处理加速
JunYingSceneFunc.onTimerEnd = function(self, timer)
  if timer~=nil then
    timer.view:getParent():removeChild(timer.view, true)
  end
  if timer==self.upBuildingTimer then
    self.timeLabel:setVisible(true)
    self.upBuildingButton:setVisible(true)
    self.upBuildingButton:setEnabled(true)
  end
end

JunYingSceneFunc.onJiaSuButton = function(self, timer, pars)
  local tmp = JiaSuSampleDialog:create(pars.leftTime, self)
  MainScene.scene:addChild(tmp.view)
  UITimer.removeUITimer(self.upBuildingTimer)
  MainScene.scene:removeChild(self.node, true)
  self.jiasuUpBuildingDlg = tmp
end
JunYingSceneFunc.onJiaSuConfirm = function(self, dlg, confirm, timereel, timeLeft)
  if confirm~=true then
    MainScene.scene:removeChild(dlg.view, true)
    MainScene.scene:addChild(JunYingScene.getNode().node)
  elseif dlg==self.jiasuUpBuildingDlg then
    --NetworkRequest.test("onJiaSuComplete", self)
    NetworkRequest.accelerate("Upgrading", BuildingID.JunYing, timereel, "onJiaSuComplete", self)
    --setTouchEnabledAll(dlg.view, false)
    LoadScene:showLoadingScene()
  end
end
JunYingSceneFunc.onJiaSuComplete = function(self, data)
  LoadScene:removeLoadingScene()
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    if self.jiasuUpBuildingDlg~=nil then
      self.upBuildingTimer:endTimer()
    end
    showMessage("加速成功", ccc3(0,255,0))
  else
    showMessage(res.msg, ccc3(255,255,0))
    --showMessage("加速失败", ccc3(255,255,0))
  end
  if self.jiasuUpBuildingDlg~=nil then
    MainScene.scene:removeChild(self.jiasuUpBuildingDlg.view, true)
  end
  MainScene.scene:addChild(JunYingScene.getNode().node)
end
JunYingSceneFunc.closeScene = function(self)
  UITimer.removeUITimer(self.upBuildingTimer)
  MainScene.scene:removeChild(self.node, true)
  JunYingScene.instance = nil
end
JunYingSceneFunc.onClose = function(self, node, event)
  print("onCloseZhiHuiBu")
  self:closeScene()
  setTouchEnabledAll(MainScene.node, true)
  MainScene.setState()
end

